package doantotnghiep.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.servlet.ModelAndView;

import doantotnghiep.business.LoginBusiness;
import doantotnghiep.business.StaffBusiness;
import doantotnghiep.model.Staff;
import doantotnghiep.modelDTO.StaffDTO;
import doantotnghiep.modelDTO.UserDTO;

@Controller
public class HomeController {
	@Autowired
	private LoginBusiness loginbusiness;
	@Autowired
	private StaffBusiness staffBusiness;

	private static int loginedUserID;
	
	private static int isTryLogined = 0;

	@ModelAttribute("UserDTO")
	public UserDTO getUserObject() {
		return new UserDTO();
	}

	@RequestMapping(value = "/", method = RequestMethod.GET)
	public ModelAndView index() {
		//staffBusiness.initAdmin();
		return new ModelAndView("homepage", "", null);
	}

	@RequestMapping(value = "/login", method = RequestMethod.GET)
	public ModelAndView login(HttpServletRequest request, ModelMap model,
			String userName, String passWord) {
		Staff staff = loginbusiness.checkValidAccount(userName, passWord);
		if (staff != null) {
			UserDTO userDTO = new UserDTO();
			userDTO.setStaffID(staff.getId());
			loginedUserID = staff.getId();
			userDTO.setUserName(staff.getUserName());
			userDTO.setStaffName(staff.getFullName());
			userDTO.setRoomID(staff.getDivisionRoom().getId());
			Map<String, Object> loginModel = new HashMap<String, Object>();
			loginModel.put("userDTO", userDTO);

			staff.setIsLogined(1);// save logined status
			staffBusiness.saveStaffChanged(staff);

			request.getSession().setAttribute("userDTO", userDTO);
			isTryLogined = 0;
			
			return new ModelAndView("homepage");
		}
		else{
			isTryLogined = 1;
			request.setAttribute("isTryLogined", isTryLogined);
		}
		return new ModelAndView("homepage");
	}

	@RequestMapping(value = "/logOut", method = RequestMethod.GET)
	public ModelAndView logOut(HttpServletRequest request) {
		Staff staff = staffBusiness.getStaffById(loginedUserID);
		if (staff != null) {
			staff.setIsLogined(0);
			staffBusiness.saveStaffChanged(staff);
			request.getSession().removeAttribute("userDTO");
			loginedUserID = 0;
			isTryLogined = 0;
			return new ModelAndView("homepage");
		}
		return new ModelAndView("homepage");
	}

	@RequestMapping(value = "/viewAllUser", method = RequestMethod.GET)
	public ModelAndView viewAllUser() {
		List<Staff> staffs = staffBusiness.listStaff();
		ArrayList<UserDTO> userDTOs = new ArrayList<UserDTO>();
		if (staffs != null) {
			for (Staff staff : staffs) {
				UserDTO userDTO = new UserDTO();

				userDTO.setStaffID(staff.getId());
				userDTO.setStaffName(staff.getFullName());
				userDTO.setUserName(staff.getUserName());

				userDTOs.add(userDTO);
			}
			Map<String, Object> userModel = new HashMap<String, Object>();
			userModel.put("userDTOs", userDTOs);
			return new ModelAndView("viewAllAccount", "userModel", userModel);
		}
		return new ModelAndView("viewAllAccount");
	}

	@RequestMapping(value = "/addUser", method = RequestMethod.GET)
	public ModelAndView addUser() {
		List<Staff> staffs = staffBusiness.listEmptyUser();
		List<StaffDTO> staffDTOs = new ArrayList<StaffDTO>();
		if (staffs != null) {
			for (Staff staff : staffs) {
				StaffDTO staffDTO = new StaffDTO();

				staffDTO.setId(staff.getId());
				staffDTO.setFullName(staff.getFullName());
				staffDTOs.add(staffDTO);
			}
			Map<String, Object> staffModel = new HashMap<String, Object>();
			staffModel.put("staffDTOs", staffDTOs);
			return new ModelAndView("addAccount", "staffModel", staffModel);
		}

		return new ModelAndView("addAccount");
	}

	@RequestMapping(value = "/addUser", method = RequestMethod.POST)
	public String addUser(HttpServletRequest request, ModelMap model,
			@ModelAttribute("UserDTO") UserDTO userDTO) {
		if (userDTO != null) {
			Staff staff = staffBusiness.getStaffById(userDTO.getStaffID());
			staff.setUserName(userDTO.getUserName());
			staff.setPassword(userDTO.getPassWord());
			staffBusiness.saveStaffChanged(staff);
		}
		return "redirect:/viewAllUser";
	}

	@RequestMapping(value = "/editPassword", method = RequestMethod.GET)
	public ModelAndView editPassword(HttpServletRequest request,
			ModelMap model, int id) {
		Staff staff = staffBusiness.getStaffById(id);
		if (staff != null) {
			UserDTO userDTO = new UserDTO();
			userDTO.setStaffID(staff.getId());
			userDTO.setUserName(staff.getUserName());
			userDTO.setStaffName(staff.getFullName());
			userDTO.setPassWord(staff.getPassword());
			return new ModelAndView("updatePassword", "userDTO", userDTO);
		}
		return new ModelAndView("homepage");
	}

	@RequestMapping(value = "/editPassword", method = RequestMethod.POST)
	public String editPassword(HttpServletRequest request, ModelMap model,
			@ModelAttribute("UserDTO") UserDTO userDTO) {
		if (userDTO != null) {
			Staff staff = staffBusiness.getStaffById(userDTO.getStaffID());
			staff.setPassword(userDTO.getPassWord());
			staffBusiness.saveStaffChanged(staff);
		}
		return "redirect:/viewAllUser";
	}

	@RequestMapping(value = "/satisticMoney", method = RequestMethod.GET)
	public ModelAndView satisticMoney() {
		return new ModelAndView("satisticMoney");
	}

}
